package org.example.petsystem.controller;

import cn.hutool.poi.excel.cell.CellSetter;
import org.example.petsystem.entity.ResultBody;
import org.example.petsystem.service.OrderManService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/OrderManagement")
@CrossOrigin(origins="*")
public class OrderManController {
    @Autowired
    private OrderManService orderManService;
    //删除订单（管理员）
    @RequestMapping("/DeOrder")
    public ResultBody DeOrder(@RequestBody Map<String,Object> map) {
        if(map.get("code")==null)
            //订单编号
            return new ResultBody(404,"参数错误",false);
        String code = map.get("code").toString();
        if(orderManService.DeOrder(code)>0)
        {
            return new ResultBody(200,"删除订单成功",true);
        }
        else
            return new ResultBody(400,"删除订单失败",false);
    }
    //展示订单详细信息（管理员）
    @RequestMapping("/displayOrder")
    public ResultBody displayOrder() {
        return orderManService.displayOrder();
    }
    //展示订单详细信息（在打算支付时）
    @RequestMapping("/displayOrderDetailed")
    public ResultBody displayOrderDetailed(@RequestBody Map<String,Object> map) {
        if(map.get("code")==null||map.get("phone")==null) {
            //商品编号，用户电话号码
            return new ResultBody(404, "参数错误", false);
        }
        String phone = map.get("phone").toString();//用户电话号码
        String code = map.get("code").toString();//商品编号
        return orderManService.displayOrderDetailed(phone,code);
    }
    //待发货订单
    @RequestMapping("/displayOrderSend")
    public ResultBody displayOrderSend(@RequestBody Map<String,Object> map) {
        if(map.get("Buyphone")==null)
        {
            return new ResultBody(404,"参数错误",false);
        }
        return orderManService.displayOrderSend(map);
    }
    //增加订单编号
    @RequestMapping("/updateStatus")
    public ResultBody updateStatus(@RequestBody Map<String,Object> map) {
        if(map.get("code")==null||map.get("Expresscode")==null)
        {
            return new ResultBody(404,"参数错误",false);
        }
        return orderManService.updateStatus(map);
    }
    //申请退款接口
    @RequestMapping("/Refund")
    public ResultBody Refund(@RequestBody Map<String,Object> map) {
        System.out.println(map);
        if(map.get("code")==null||map.get("userphone")==null||map.get("Refundtype")==null||map.get("Refundreason")==null||map.get("Refundprice")==null||map.get("Refundquestion")==null||map.get("avatarUrl")==null)
        {
            return new ResultBody(404,"参数错误",false);
        }
        int  Refundtype = (int)map.get("Refundtype");
        return switch (Refundtype) {
            case 1 -> orderManService.Refund(map);
            case 2 -> {
                if (map.get("Expresscode") == null||orderManService.checkExpresscode(map)>0) {
                    yield new ResultBody(404, "快递单号错误", false);
                }
                yield orderManService.Refund(map);
            }
            default -> new ResultBody(500, "未知类型", false);
        };
    }
    //我的退款订单（已同意，未同意）（买家端）
    @RequestMapping("/displayRefundUser")
    public ResultBody displayRefundUser(@RequestBody Map<String,Object> map) {
        if(map.get("userphone")==null)
        {
            return new ResultBody(404,"参数错误",false);
        }
        return orderManService.displayRefundUser(map);
    }
    //显示退款订单（管理员端）
    @RequestMapping("/displayRefund")
    public ResultBody displayRefund() {
        return orderManService.displayRefund();
    }
    //审核待退款订单
    @RequestMapping("/checkRefund")
    public ResultBody checkRefund(@RequestBody Map<String,Object> map) {
        if(map.get("code")==null||map.get("decision")==null)
        {
            return new ResultBody(404,"参数错误",false);
        }
        // 如果是拒绝退款，必须提供拒绝理由
        if("拒绝退款".equals(map.get("decision")) && map.get("reject_reason") == null) {
            return new ResultBody(404,"拒绝退款时必须提供拒绝理由",false);
        }
        return orderManService.checkRefund(map);
    }


}
